package com.czh.gzskzn.qqkonjian;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czh.gzskzn.qqkonjian.Fragment.FragmentXiaoXi;
import com.czh.gzskzn.qqkonjian.Fragment.FragmentGeRen;
import com.czh.gzskzn.qqkonjian.Fragment.FragmentYinYon;


public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final String TAG = "MainActivity";
//    private ImageView ivImg;

//    private RelativeLayout rlClick;
    private Context context;


    // 定义Fragment页面
    private FragmentXiaoXi fragmentXiaoXi;
    private FragmentGeRen fragmentGeRen;;
    private FragmentYinYon fragmentYinYon;

    // 定义图片组件对象
    private ImageView atIv,  moreIv, imbYinYon;
    private TextView xiaoxizhonxin,gerenzhonxin, tvYinYon;
    private LinearLayout xiaoxi,geren,llYonYON;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();


    }

    private void initViews() {
        context = this;
//        ivImg = (ImageView) findViewById(R.id.iv_img);
//        rlClick = (RelativeLayout) findViewById(R.id.rl_click);
        xiaoxi = (LinearLayout) findViewById(R.id.xiaoxi);
        geren = (LinearLayout) findViewById(R.id.geren);
        llYonYON = (LinearLayout) findViewById(R.id.ll_yinyon);
        xiaoxizhonxin = (TextView) findViewById(R.id.xiaoxizhonxin);
        gerenzhonxin = (TextView) findViewById(R.id.gerenzzhonxin);
        tvYinYon = (TextView) findViewById(R.id.tv_yinyon);
        atIv = (ImageView) findViewById(R.id.image_at);
        moreIv = (ImageView) findViewById(R.id.image_more);
        imbYinYon = (ImageView) findViewById(R.id.imb_yinyon);
        clickAtBtn();

        xiaoxi.setOnClickListener(this);
        geren.setOnClickListener(this);
        llYonYON.setOnClickListener(this);
        atIv.setOnClickListener(this);
        moreIv.setOnClickListener(this);
        imbYinYon.setOnClickListener(this);

    }

//    消息页
    private void clickAtBtn() {
        // 实例化Fragment页面
        fragmentXiaoXi = new FragmentXiaoXi();
        // 得到Fragment事务管理器
        FragmentTransaction fragmentTransaction = this
                .getSupportFragmentManager().beginTransaction();
        // 替换当前的页面
        fragmentTransaction.replace(R.id.frame_content, fragmentXiaoXi);
        // 事务管理提交
        fragmentTransaction.commit();
        atIv.setImageResource(R.drawable.message);
        xiaoxizhonxin.setTextColor(Color.parseColor("#FF0C97FF"));

        // 改变选中状态
        xiaoxi.setSelected(true);
        atIv.setSelected(true);
        geren.setSelected(false);
        moreIv.setSelected(false);
        imbYinYon.setSelected(false);
        llYonYON.setSelected(false);
    }
    /**
     * 点击了“我的”按钮
     */
    private void clickMoreBtn() {
        // 实例化Fragment页面
        fragmentGeRen = new FragmentGeRen();
        // 得到Fragment事务管理器
        FragmentTransaction fragmentTransaction = this
                .getSupportFragmentManager().beginTransaction();
        // 替换当前的页面
        fragmentTransaction.replace(R.id.frame_content, fragmentGeRen);
        // 事务管理提交
        fragmentTransaction.commit();


        xiaoxi.setSelected(false);
        atIv.setSelected(false);
        geren.setSelected(true);
        moreIv.setSelected(true);
        imbYinYon.setSelected(false);
        llYonYON.setSelected(false);
    }
/**
 *      应用页
 * */
private void clickYinYon() {
    // 实例化Fragment页面
    fragmentYinYon = new FragmentYinYon();
    // 得到Fragment事务管理器
    FragmentTransaction fragmentTransaction = this
            .getSupportFragmentManager().beginTransaction();
    // 替换当前的页面
    fragmentTransaction.replace(R.id.frame_content, fragmentYinYon);
    // 事务管理提交
    fragmentTransaction.commit();
    // 改变选中状态
    xiaoxi.setSelected(false);
    atIv.setSelected(false);
    geren.setSelected(false);
    moreIv.setSelected(false);
    imbYinYon.setSelected(true);
    llYonYON.setSelected(true);
}


    private void clearSelection() {
        atIv.setImageResource(R.drawable.xiaoxi2);
       xiaoxizhonxin .setTextColor(Color.parseColor("#82858b"));
        moreIv.setImageResource(R.drawable.my);
        gerenzhonxin.setTextColor(Color.parseColor("#82858b"));
        imbYinYon.setImageResource(R.drawable.yinon);
        tvYinYon.setTextColor(Color.parseColor("#82858b"));


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.image_at:
//                消息
                clearSelection();
                clickAtBtn();
                atIv.setImageResource(R.drawable.message);
                xiaoxizhonxin.setTextColor(Color.parseColor("#FF0C97FF"));
                break;
            case R.id.image_more:
//                个人
                clearSelection();
                clickMoreBtn();
                moreIv.setImageResource(R.drawable.my2);
                gerenzhonxin.setTextColor(Color.parseColor("#FF0C97FF"));
                break;
            case R.id.imb_yinyon:
//                应用
                clearSelection();
                clickYinYon();
                imbYinYon.setImageResource(R.drawable.yinon);
                tvYinYon.setTextColor(Color.parseColor("#FF0C97FF"));
                break;

            case R.id.xiaoxi:
//                消息
                clearSelection();
                clickAtBtn();
                atIv.setImageResource(R.drawable.message);
                xiaoxizhonxin.setTextColor(Color.parseColor("#FF0C97FF"));
                break;
            case R.id.geren:
//                个人
                clearSelection();
                clickMoreBtn();
                moreIv.setImageResource(R.drawable.my2);
                gerenzhonxin.setTextColor(Color.parseColor("#FF0C97FF"));
                break;
            case R.id.ll_yinyon:
//                应用
                clearSelection();
                clickYinYon();
                imbYinYon.setImageResource(R.drawable.yinon);
                tvYinYon.setTextColor(Color.parseColor("#FF0C97FF"));
                break;

        }
    }
}
